package com.example.day20_zhongji.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day20_zhongji.R;
import com.example.day20_zhongji.bean.ArticleBean;

import java.util.List;

public class CradAdapter extends RecyclerView.Adapter<CradAdapter.ViewHolder> {
    private Context context;
    private List<ArticleBean.DataBean.DatasBean> data;
    private int a=0;
    private int b=1;
    private OnContextClickListener onContextClickListener;

    public CradAdapter(Context context, List<ArticleBean.DataBean.DatasBean> data) {
        this.context = context;
        this.data = data;
    }

    public interface OnContextClickListener{
        void oncontextclicklistener(View v,int position);
    }

    public void setOnContextClickListener(OnContextClickListener onContextClickListener) {
        this.onContextClickListener = onContextClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==a){
            View inflate = LayoutInflater.from(context).inflate(R.layout.crad_item, parent, false);
            return new ViewHolder(inflate);
        }else if (viewType==b){
            View inflate = LayoutInflater.from(context).inflate(R.layout.crad_item1, parent, false);
            return new ViewHolder1(inflate);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ArticleBean.DataBean.DatasBean datasBean = data.get(position);
        if (getItemViewType(position)==a){
            holder.text.setText(datasBean.getShareUser());
        }else if (getItemViewType(position)==b){
            ViewHolder1 holder1= (ViewHolder1) holder;
            holder1.text1.setText(datasBean.getShareUser());
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onContextClickListener.oncontextclicklistener(view,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position%2==0){
            return a;
        }else{
            return b;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView text;
        ImageView iamge;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text=itemView.findViewById(R.id.card_text);
            iamge=itemView.findViewById(R.id.card_image);
        }
    }

    private class ViewHolder1 extends ViewHolder {
        TextView text1;
        ImageView image;
        public ViewHolder1(@NonNull View itemView) {
            super(itemView);
            text1=itemView.findViewById(R.id.card_text1);
            image=itemView.findViewById(R.id.card_image1);
        }
    }
}
